//
//  ZYLSearchTypeCollectionView.m
//  Cartoon
//
//  Created by dllo on 15/7/14.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "ZYLSearchTypeCollectionView.h"
#import "Define.h"
#import "ZYLSearchTypeCollectionViewCell.h"
@implementation ZYLSearchTypeCollectionView 

- (instancetype)initWithFrame:(CGRect)frame
{
    UICollectionViewFlowLayout *layout = [[[UICollectionViewFlowLayout alloc] init] autorelease];
    layout.sectionInset = UIEdgeInsetsMake(0, 20, 20, 20);
    self = [super initWithFrame:frame collectionViewLayout:layout];
    if (self) {
        [self createTypeCollectionViewWithFrame:frame collectionViewLayout:layout];
    }
    return self;
}

- (void)createTypeCollectionViewWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewFlowLayout *)layout
{
    self.delegate = self;
    self.dataSource = self;
    self.backgroundColor = [UIColor whiteColor];
    layout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 40);
    [self registerClass:[ZYLSearchTypeCollectionViewCell class] forCellWithReuseIdentifier:@"ZYLSearchTypeCollectionViewCell"];
    [self registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ZYLSearchTypeCollectionViewHeader"];
}


- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (kind == UICollectionElementKindSectionHeader) {
        UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"ZYLSearchTypeCollectionViewHeader" forIndexPath:indexPath];
        UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(20, 0, header.frame.size.width - 40, header.frame.size.height)] autorelease];
        label.text = @"按类别搜索";
        label.textColor = [UIColor lightGrayColor];
        [header addSubview:label];
        return header;
    }
    return nil;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZYLSearchTypeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZYLSearchTypeCollectionViewCell" forIndexPath:indexPath];
    cell.labelText = self.typeLabelTextArr[indexPath.item];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGSize titleSize = [self.typeLabelTextArr[indexPath.item] sizeWithFont:[UIFont systemFontOfSize:15] constrainedToSize:CGSizeMake(MAXFLOAT, 30)];
    return CGSizeMake(titleSize.width + 20, 30);
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.typeLabelTextArr.count;
}


@end
